package com.mlink.ai.chat.ads

import com.adsdk.android.ads.nativead.NativeAdListener
import com.adsdk.android.ads.nativead.OxNativeAdHelper
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.nativead.NativeAd
import com.mlink.ai.chat.AiChatApplication

val adManager = AdManager()

class AdManager {

    private var mainNativeBannerAd: OxNativeAdHelper? = null
    private var mainNativeBannerLoadState = LoadState.IDLE

    private var languageNativeAd: NativeAd? = null

    fun loadMainNativeBannerAd() {
        val adConfig = AdUtils.getMainNativeBannerAdConfig(AiChatApplication.app)
        mainNativeBannerAd = createNativeAd(adConfig.adId)
        mainNativeBannerAd?.run {
            setAdListener(object : NativeAdListener() {
                override fun onAdLoaded() {
                    super.onAdLoaded()
                    mainNativeBannerLoadState = LoadState.LOAD_SUCCESS
                }

                override fun onAdLoadFailed(p0: String?, p1: String?) {
                    super.onAdLoadFailed(p0, p1)
                    mainNativeBannerLoadState = LoadState.LOAD_FAILED
                }
            })
            mainNativeBannerLoadState = LoadState.LOADING
            loadAd(adConfig.adPlacement)
        }
    }

    fun loadLanguageNativeAd() {
        val adLoader =
            AdLoader.Builder(AiChatApplication.app, AdMobAdConfig.LANGUAGE_NATIVE_AD.adId)
                .forNativeAd {
                    this.languageNativeAd = it
                    languageNativeAd?.setOnPaidEventListener { adValue ->
                        val adNetwork =
                            languageNativeAd?.responseInfo?.mediationAdapterClassName ?: "unknown"
                        OxAdValueUtils.logAdmobImpressionRevenue(
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adId,
                            "native",
                            adNetwork,
                            adValue,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adPlacement
                        )
                    }
                }
                .withAdListener(object : AdListener() {
                    override fun onAdImpression() {
                        super.onAdImpression()
                        val networkName: String? =
                            languageNativeAd?.responseInfo?.mediationAdapterClassName
                        AdEventUtil.trackAdImpressionEvent(
                            AdFormat.NATIVE,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adId,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adPlacement,
                            null,
                            networkName,
                            null,
                            0,
                            0,
                            null,
                            0.0
                        )
                    }

                    override fun onAdLoaded() {
                        super.onAdLoaded()
                        val networkName: String? =
                            languageNativeAd?.responseInfo?.mediationAdapterClassName
                        AdEventUtil.trackAdLoadedEvent(
                            AdFormat.NATIVE,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adId,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adPlacement,
                            0, null, networkName, null, 0, 0, null, 0.0
                        )
                    }

                    override fun onAdFailedToLoad(p0: LoadAdError) {
                        super.onAdFailedToLoad(p0)
                        AdEventUtil.trackAdLoadFailedEvent(
                            AdFormat.NATIVE,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adId,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adPlacement, p0.message, 0
                        )
                    }

                    override fun onAdClicked() {
                        super.onAdClicked()
                        val networkName: String? =
                            languageNativeAd?.responseInfo?.mediationAdapterClassName
                        AdEventUtil.trackAdClickEvent(
                            AdFormat.NATIVE,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adId,
                            AdMobAdConfig.LANGUAGE_NATIVE_AD.adPlacement,
                            null,
                            networkName,
                            null,
                            0,
                            null,
                            0.0
                        )
                    }
                })
                .build()
        AdEventUtil.trackAdRequestEvent(
            AdFormat.NATIVE,
            AdMobAdConfig.LANGUAGE_NATIVE_AD.adId,
            AdMobAdConfig.LANGUAGE_NATIVE_AD.adPlacement
        )
        adLoader.loadAd(AdRequest.Builder().build())
    }

    private fun createNativeAd(adId: String): OxNativeAdHelper? {
        if (AiChatApplication.app.appViewModel.isSubscribed()) {
            return null
        }
        return OxNativeAdHelper.createAd(AiChatApplication.app, adId)
    }

    fun getMainNativeBannerAd(): OxNativeAdHelper? {
        if (mainNativeBannerLoadState == LoadState.LOAD_SUCCESS) {
            return mainNativeBannerAd
        }
        return null
    }

    fun getLanguageNativeAd(): NativeAd? = languageNativeAd

    fun destroyMainNativeBannerAd() {
        mainNativeBannerAd?.destroyAd()
        mainNativeBannerAd = null
    }

    fun destroyLanguageNativeAd() {
        languageNativeAd?.destroy()
        languageNativeAd = null
    }

    enum class LoadState {
        IDLE, LOADING, LOAD_FAILED, LOAD_SUCCESS
    }
}